package ru.amse.tsyganov.jumleditor.properties;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Table;

public abstract class Property<T> {
		
	private final Set<PropertyChangeListener> propertyChangeListeners = 
		new HashSet<PropertyChangeListener>();
	
	private final String name;
	
	private CellEditor cellEditor;
	
	public Property(String name) {
		this.name = name;
	}
	
	public abstract T getPropertyValue();
	
	public abstract void setPropertyValue(T newValue);
	
	public final CellEditor getCellEditor(Table table) {
		if (cellEditor == null) {
			cellEditor = createCellEditor(table);
		}
		return cellEditor;
	}
	
	public abstract CellEditor createCellEditor(Table table);
	
	public final String getPropertyName() {
		return name;
	}

	public void setPropertyValueAndNotify(T newValue) {
		if (!getPropertyValue().equals(newValue)) {
			setPropertyValue(newValue);
			firePropertyChanged(this);
		}
	}
	
	public String getStringPropertyValue() {
		return getPropertyValue().toString();
	}
	
	public void firePropertyChanged(Property p) {
		for (PropertyChangeListener listener : 
			propertyChangeListeners) {
			
			listener.propertyChanged(p);
		}
	}

	public void removeChangeListener(
			PropertyChangeListener listener) {
		
		propertyChangeListeners.remove(listener);
	}

	public void addChangeListener(
			PropertyChangeListener listener) {
		
		propertyChangeListeners.add(listener);
	}
	
	public Image getImage() {
		return null;
	}
	
	public boolean canModify() {
		return true;
	}
}
